package com.actions.bluetoothbox2.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.bluetoothbox2.util.Utils;
import com.actions.bluetoothbox2.util.slideLeftAdapter;
import com.actions.bluetoothbox2.util.slideTitleInfo;
import com.actions.ibluz.manager.BluzManager;
import com.actions.ibluz.manager.BluzManagerData;
import com.actions.ibluz.manager.IGlobalManager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

//妈妈自选
public class MatherSelectionFragment extends Fragment {

    private static final String LOGTAG = "MatherSelectionFragment";
    private GridView mather_grid;
    private List<slideTitleInfo> list;
    private BrowserActivity mActivity;
    private BluzManager mBluzManager;
    private slideLeftAdapter leftadapter;
    private boolean islocked = false;

    public void lineinMenuChanged(boolean visibility) {
        int visible = visibility ? View.VISIBLE : View.GONE;
//        mLineinBtnLayout.setVisibility(visible);
        list.get(2).visibility = visibility ? 0 : 1;
        if (list.get(2).visibility == 1) {
            list.get(2).img_id = R.drawable.ic_aux_gone;
        } else {
            list.get(2).img_id = R.drawable.ic_aux;
        }
        leftadapter.notifyDataSetChanged();
    }

    public void cardMenuChanged(boolean visibility) {
        int visible = visibility ? View.VISIBLE : View.GONE;
        list.get(3).visibility = visibility ? 0 : 1;
        list.get(3).visibility = visibility ? 0 : 1;

        if (list.get(3).visibility == 1) {
            list.get(3).img_id = R.drawable.ic_sd_false;
        } else {
            list.get(3).img_id = R.drawable.ic_sd;
        }
        leftadapter.notifyDataSetChanged();
    }

    public MatherSelectionFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mather_selection, container, false);
        mather_grid = (GridView) view.findViewById(R.id.mather_grid);
        mActivity = (BrowserActivity) getActivity();
        mBluzManager = mActivity.getBluzManager();

        mActivity.tv_title.setText(getResources().getText(R.string.mather_select));
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.title_back.setVisibility(View.VISIBLE);
        mActivity.mGlobalInfoLayout.setVisibility(View.GONE);

        mActivity.title_back.setOnClickListener(mActivity.click);

        initList();
        leftadapter = new slideLeftAdapter(getActivity(), list);
        mather_grid.setAdapter(leftadapter);
        mather_grid.setOnItemClickListener(listener);
        lineinMenuChanged(mActivity.is_AUX);
        cardMenuChanged(mActivity.is_TF);
        IsLoked();
        return view;
    }

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Log.i("TAG", "MatherSelectionFragment: ----" + list.get(position).visibility);
            int mode = -2;
            switch (position) {
                case 0:
                    mode = BluzManagerData.FuncMode.A2DP;
                    break;
                case 1:
                    mode = 14;
                    break;
                case 2:
                    if (list.get(2).visibility == 0) {
                        mode = BluzManagerData.FuncMode.LINEIN;
                    }
                    break;
                case 3:
                    if (list.get(3).visibility == 0) {
                        mode = BluzManagerData.FuncMode.CARD;
                    }
                    break;
                case 4:
                    int cmd_type = Integer.decode("0x53");
                    int cmd_id = Integer.decode("0x9d");
                    islocked = !islocked;
                    if (islocked) {
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 1, 0, null);
                        list.get(4).img_id = R.drawable.ic_locked;
                    } else {
                        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, null);
                        list.get(4).img_id = R.drawable.ic_lock;
                    }
                    leftadapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
            if (mActivity.isConnection && mode != -2) {
                if (list.get(position).visibility == 0) {
                    mActivity.title_back.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            MatherSelectionFragment fragment = new MatherSelectionFragment();
                            mActivity.replaceFragment(fragment, SlideLeftFragment.FRAGMENT_TAG_MATHER);
                        }
                    });
                    mActivity.setMode(mode);
                }
            }
        }
    };

    private void initList() {
        list = new ArrayList<>();

        slideTitleInfo info_1 = new slideTitleInfo();
        info_1.img_id = R.drawable.ic_bluetooth;
        info_1.str = getResources().getString(R.string.bluetoothpush);
        info_1.id = 1;
        list.add(info_1);

        slideTitleInfo info_2 = new slideTitleInfo();
        info_2.img_id = R.drawable.ic_led;
        info_2.str = getResources().getString(R.string.led);
        info_2.id = 2;
        list.add(info_2);

        slideTitleInfo info_3 = new slideTitleInfo();
        info_3.img_id = R.drawable.ic_aux;
        info_3.str = getResources().getString(R.string.linein);
        info_3.id = 3;
        list.add(info_3);

        slideTitleInfo info_4 = new slideTitleInfo();
        info_4.img_id = R.drawable.ic_sd;
        info_4.str = getResources().getString(R.string.cardsongplay);
        info_4.id = 4;
        list.add(info_4);

        slideTitleInfo info_5 = new slideTitleInfo();
        info_5.img_id = R.drawable.ic_lock;
        info_5.str = getResources().getString(R.string.lock);
        info_5.id = 5;
        list.add(info_5);

    }

    private void IsLoked() {
        mBluzManager.setOnCustomCommandListener(new BluzManagerData.OnCustomCommandListener() {

            @Override
            public void onReady(int i, int i1, int i2, byte[] bytes) {
                int type = Utils.bytesToInt(bytes, 0);
                Log.i("TAG", "onReady: ----type == " + type);
                if (i1 == 1) {
                    islocked = true;
                    list.get(4).img_id = R.drawable.ic_locked;
                } else {
                    islocked = false;
                    list.get(4).img_id = R.drawable.ic_lock;
                }
            }
        });
        int cmd_type = Integer.decode("0x51");
        int cmd_id = Integer.decode("0x81");
        mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 2, 0, null);
    }
}
